import 'package:flutter/material.dart';
import 'package:flutter_gallery/demos/material/material_demo_types.dart';
import 'package:flutter_gen/gen_l10n/gallery_localizations.dart';
import 'package:fluttertoast/fluttertoast.dart';

class ButtonDemo extends StatelessWidget {
  const ButtonDemo({Key? key, required this.type}) : super(key: key);

  final ButtonDemoType type;

  String _title(BuildContext context) {
    switch (type) {
      case ButtonDemoType.text:
        return GalleryLocalizations.of(context)!.demoTextButtonTitle;
      case ButtonDemoType.elevated:
        return GalleryLocalizations.of(context)!.demoElevatedButtonTitle;
      case ButtonDemoType.outlined:
        return GalleryLocalizations.of(context)!.demoOutlinedButtonTitle;
      case ButtonDemoType.toggle:
        return GalleryLocalizations.of(context)!.demoToggleButtonTitle;
      case ButtonDemoType.floating:
        return GalleryLocalizations.of(context)!.demoFloatingButtonTitle;
    }
  }

  @override
  Widget build(BuildContext context) {
    Widget buttons;

    switch (type) {
      case ButtonDemoType.text:
        buttons = const _TextButtonDemo();
        break;
      case ButtonDemoType.elevated:
        buttons = const _ElevatedButtonDemo();
        break;
      case ButtonDemoType.outlined:
        buttons = const _OutlinedButtonDemo();
        break;
      case ButtonDemoType.toggle:
        buttons = const _ToggleButtonsDemo();
        break;
      case ButtonDemoType.floating:
        buttons = const _FloatingActionButtonDemo();
        break;
    }

    return Scaffold(
      appBar: AppBar(
        automaticallyImplyLeading: false,
        title: Text(_title(context)),
      ),
      body: buttons,
    );
  }
}

// BEGIN buttonDemoText
class _TextButtonDemo extends StatelessWidget {
  const _TextButtonDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          TextButton(
            onPressed: () {
              Fluttertoast.showToast(msg: 'buttonDemoText');
            },
            child: Text(GalleryLocalizations.of(context)!.buttonText),
          ),
          const SizedBox(height: 12),
          TextButton.icon(
            onPressed: () {
              Fluttertoast.showToast(msg: 'buttonDemoText icon');
            },
            icon: const Icon(Icons.add, size: 18),
            label: Text(GalleryLocalizations.of(context)!.buttonText),
          ),
        ],
      ),
    );
  }
}

// BEGIN buttonDemoElevated
class _ElevatedButtonDemo extends StatelessWidget {
  const _ElevatedButtonDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          ElevatedButton(
            onPressed: () {
              Fluttertoast.showToast(msg: 'buttonDemoElevated');
            },
            child: Text(GalleryLocalizations.of(context)!.buttonText),
          ),
          const SizedBox(height: 12),
          ElevatedButton.icon(
              onPressed: () {
                Fluttertoast.showToast(msg: 'buttonDemoElevated icon');
              },
              icon: const Icon(
                Icons.add,
                size: 18,
              ),
              label: Text(GalleryLocalizations.of(context)!.buttonText)),
        ],
      ),
    );
  }
}

// BEGIN buttonDemoOutlined
class _OutlinedButtonDemo extends StatelessWidget {
  const _OutlinedButtonDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          OutlinedButton(
            onPressed: () {
              Fluttertoast.showToast(msg: 'buttonDemoOutlined');
            },
            child: Text(GalleryLocalizations.of(context)!.buttonText),
          ),
          const SizedBox(height: 12),
          OutlinedButton.icon(
            icon: const Icon(Icons.add, size: 18),
            label: Text(GalleryLocalizations.of(context)!.buttonText),
            onPressed: () {
              Fluttertoast.showToast(msg: 'buttonDemoOutlined icon');
            },
          ),
        ],
      ),
    );
  }
}

// BEGIN buttonDemoToggle
class _ToggleButtonsDemo extends StatefulWidget {
  const _ToggleButtonsDemo({Key? key}) : super(key: key);

  @override
  _ToggleButtonsDemoState createState() => _ToggleButtonsDemoState();
}

class _ToggleButtonsDemoState extends State<_ToggleButtonsDemo>
    with RestorationMixin {
  final isSelected = [
    RestorableBool(false),
    RestorableBool(true),
    RestorableBool(false),
  ];

  @override
  String? get restorationId => 'toggle_button_demo';

  @override
  void restoreState(RestorationBucket? oldBucket, bool initialRestore) {
    registerForRestoration(isSelected[0], 'first_item');
    registerForRestoration(isSelected[1], 'second_item');
    registerForRestoration(isSelected[2], 'third_item');
  }

  @override
  void dispose() {
    for (final restorableBool in isSelected) {
      restorableBool.dispose();
    }
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Center(
      child: ToggleButtons(
        children: const [
          Icon(Icons.ac_unit),
          Icon(Icons.call),
          Icon(Icons.cake),
        ],
        isSelected: isSelected.map((e) => e.value).toList(),
        onPressed: (index) {
          setState(() {
            isSelected[index].value = !isSelected[index].value;
          });
        },
      ),
    );
  }
}

// BEGIN buttonDemoFloating
class _FloatingActionButtonDemo extends StatelessWidget {
  const _FloatingActionButtonDemo({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          FloatingActionButton(
            onPressed: () {
              Fluttertoast.showToast(msg: 'buttonDemoFloating');
            },
            tooltip: GalleryLocalizations.of(context)!.buttonTextCreate,
            child: const Icon(Icons.add),
          ),
          const SizedBox(height: 20),
          FloatingActionButton.extended(
            onPressed: () {
              Fluttertoast.showToast(msg: 'buttonDemoFloating icon');
            },
            label: Text(GalleryLocalizations.of(context)!.buttonTextCreate),
            icon: const Icon(Icons.add),
          ),
        ],
      ),
    );
  }
}
